'''
Created on Aug 26, 2010

@author: adolfo
'''
import array
import logging
import unittest
from mx.org.came import codec
from mx.org.came.schema_reader import ApplicationReader, FormReader
from springpython.context import ApplicationContext
from mx.org.came.tests.core_test_application_context import CoreTestConfig

_LOGGER = logging.getLogger("mx.org.came.tests.core_test")

# Pylint reports that we access protected members as an error (in these tests
# we do that on purpose.
#pylint: disable=W0212
class CodecTest(unittest.TestCase):
    """Class that defines tests for the methods in the module
    ``codec``"""

    # Spring application context with the objects to be used in the
    # unit test suite.
    CONTEXT = ApplicationContext(CoreTestConfig())

    def setUp(self):
        """Does nothing."""
        pass
    
    def tearDown(self):
        """Does nothing"""
        pass

    def test_decode(self):
        """Verifies that the ``decode`` function retrieves the query data
        properly.
        """
        query_byte_array = CodecTest.CONTEXT.get_object(
                "single_packet_session").received_packets[0][9:]
        application_dictionary = CodecTest.CONTEXT.get_object(
                "application_reader").parseFile(None)
        form_dictionary = CodecTest.CONTEXT.get_object(
                "form_reader").parseFile(None, None)
        expected_dictionary = {'schemaName': 'Client'
                               , 'Group ID': '=25512'
                               , 'dbName': 'CAME'
                               , 'logical': 'AND'}
        self.assertEquals(codec.decode(application_dictionary
                                       , form_dictionary
                                       , query_byte_array)
                , expected_dictionary
                , "Integer equality queries are not being decoded correctly!")

    def test_encode_fetch_result(self):
        """Verifies that the ``encode_fetch_result`` function returns a properly
        formatted byte array.
        """
        result_list = [(9999999L, 25513L, 50000L, 5000L, 1L, 60L)]
        application_dictionary = CodecTest.CONTEXT.get_object(
                "application_reader").parseFile(None)
        form_dictionary = CodecTest.CONTEXT.get_object(
                "form_reader").parseFile(None, None)
        expected_array = array.array("B"
                                     , [0, 0, 0, 6, 152, 150, 127, 99
                                        , 169, 0, 195, 80, 4, 226, 0, 19, 192])
        self.assertEquals(codec.encode_fetch_result(result_list
                                                    , application_dictionary
                                                    , form_dictionary)
                , expected_array
                , "Integer result-lists are not being encoded correctly!")

    def test_set_bits(self):
        """Verifies that the ``set_bits`` function assigns the bits correctly.
        """
        expected_result = array.array("B", [0xA, 0x30, 0])
        value = 0xA3 # the value to append after the current_bit_position
        current_bit_position = 3
        len_bits = 9 # 9 bits instead of just one byte (add a 0 to the left)
        ret_array = array.array("B", [0, 0, 0])
        codec.set_bits(ret_array, current_bit_position, len_bits, value)
        self.assertEquals(ret_array
                          , expected_result
                          , "The bits are not being inserted correctly!")
